<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2017/10/30
 * Time: 15:25
 */

namespace Home\Model;


use Think\Model;

class ShareUserModel extends Model
{

    public $connection = "DB_CONFIG1";
    public $tablePrefix = 'xrk_';


    /**
     * 扫描二维码关注公众号
     * @param string $qrSign
     * @param $openId
     * @return mixed
     */
    public function addShare($qrSign = "", $openId){

        $signValue = "";

        if ($qrSign){

            $qrSign = explode('_', $qrSign);
            if ($qrSign[0] == "qrscene"){

                $signValue = $qrSign[2];
            } else{

                $signValue = $qrSign[1];
            }
        }

        $where['open_id'] = $openId;
        $where['share_id'] = $signValue;
        $shareUser = $this->where($where)->find();
        if (!$shareUser){

            //增加次数
            $shareSign  = M('ShareSign','xrk_', "DB_CONFIG1");
            $where['id'] = $signValue;
            $shareSign->where($where)->setInc('share_times', 1);

            $data['share_id'] = $signValue;
            $data['add_time'] = time();
            $data['open_id'] = $openId;
            $addRes = $this->add($data);
            if ($addRes){

                return resReturn(200, 'success');
            } else{

                return resReturn(151, 'fail');
            }

        } else{

            return resReturn(150, "this user has subscribe");
        }

    }

}